//
//  TIMTextMessageCell.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/29.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMTextMessageCell.h"

#import "Masonry.h"
#import "TIMDemoMacro.h"

#define     MSG_SPACE_TOP       10
#define     MSG_SPACE_BTM       10
#define     MSG_SPACE_LEFT      10
#define     MSG_SPACE_RIGHT     15

@interface TIMTextMessageCell ()

@property (nonatomic, strong) UILabel *messageLabel;

@end

@implementation TIMTextMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.messageLabel];
    }
    return self;
}

- (void)setMessage:(TIMMessage *)message
{
    TIMMessageOwnerType lastOwnType = self.message ? self.message.ownerTyper : -1;
    [super setMessage:message];
    self.messageLabel.text = message.text;
    
    [self.messageLabel setContentCompressionResistancePriority:500 forAxis:UILayoutConstraintAxisHorizontal];
    [self.messageBackgroundView setContentCompressionResistancePriority:100 forAxis:UILayoutConstraintAxisHorizontal];
    if (lastOwnType != message.ownerTyper) {
        if (message.ownerTyper == TIMMessageOwnerTypeSelf) {
            self.messageLabel.textColor = [UIColor whiteColor];

            [self.messageBackgroundView setImage:[UIImage imageNamed:@"message_sender_bg"]];
            [self.messageBackgroundView setHighlightedImage:[UIImage imageNamed:@"message_sender_bgHL"]];
            
            [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.messageBackgroundView.mas_right).mas_offset(-MSG_SPACE_RIGHT);
                make.top.mas_equalTo(self.messageBackgroundView.mas_top).mas_offset(MSG_SPACE_TOP);
            }];
            [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.messageLabel.mas_left).mas_offset(-MSG_SPACE_LEFT);
                make.bottom.mas_equalTo(self.messageLabel.mas_bottom).mas_offset(MSG_SPACE_BTM);
            }];
            
        } else if (message.ownerTyper == TIMMessageOwnerTypeFriend) {
            
            self.messageLabel.textColor = RGBColor(51, 51, 51);
            
            [self.messageBackgroundView setImage:[UIImage imageNamed:@"message_receiver_bg"]];
            [self.messageBackgroundView setHighlightedImage:[UIImage imageNamed:@"message_receiver_bgHL"]];
    
            [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.messageBackgroundView.mas_left).mas_offset(MSG_SPACE_RIGHT);
                make.top.mas_equalTo(self.messageBackgroundView.mas_top).mas_offset(MSG_SPACE_TOP);
            }];
            [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.messageLabel.mas_right).mas_offset(MSG_SPACE_RIGHT);
                make.bottom.mas_equalTo(self.messageLabel.mas_bottom).mas_offset(MSG_SPACE_BTM);
            }];
            
            
            self.activityView.hidden = YES;
            self.retryButton.hidden = YES;
        }
    }
    // 3.1 设置文本最大尺寸
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40 - 10 * 10, MAXFLOAT);
    // 3.2 计算文本真实尺寸
    CGSize textRealSize = [self sizeWithFont:[UIFont systemFontOfSize:15] maxSize:textMaxSize string:message.text];
    
    
    [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(textRealSize);
    }];
    if (message.ownerTyper == TIMMessageOwnerTypeSelf) {
        switch (message.sendState) {
            case TIMMessageSending:
            {
                [self.activityView setHidden:NO];
                [self.retryButton setHidden:YES];
                [self.activityView startAnimating];
            }
                break;
            case TIMMessageSendSuccess:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:YES];
                
            }
                break;
            case TIMMessageSendFail:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:NO];
            }
                break;
            default:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:YES];
            }
                break;
        }
        
    }
}

/** 测量文本的尺寸 */
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string {
    NSDictionary *attrs = @{NSFontAttributeName: font};
    CGSize size =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    return size;
}


#pragma mark - Getter -
- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc] init];
        [_messageLabel setFont:TPSFontSize(14)];
        [_messageLabel setNumberOfLines:0];
    }
    return _messageLabel;
}

@end
